﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Ecreate.Web.popedom.groupManage
{
    public partial class groupCrud : System.Web.UI.Page
    {
        Ecreate.Popedom.Service.IGroupService groupService = new Ecreate.Popedom.Service.SpringImpl.GroupServiceImpl();
        Ecreate.Popedom.Service.IGroupLevel levelservice = new Ecreate.Popedom.Service.SpringImpl.GroupLevelServiceImpl();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {

                this.hid_sendurl.Value = this.Request.UrlReferrer.AbsoluteUri;
                string action = this.Request.QueryString["action"];
                string id = this.Request.QueryString["id"];
                this.hid_action.Value = action;
                this.hid_id.Value = id;
                if (action == ActionType.delete.ToString())
                {
                    Ecreate.Popedom.tGroup group = groupService.getById(long.Parse(this.hid_id.Value));
                    if (group.Adminflag == 1)
                    {
                        this.Response.Redirect(this.hid_sendurl.Value);
                        return;
                    }
                    IList<Ecreate.Popedom.tSysuser> users = group.Users;
                    if (users.Count > 0)
                    {
                        this.Response.Redirect(this.hid_sendurl.Value);
                        return;
                    }
                    else
                    {
                        group.Users = null;
                        group.Assigns = null;
                        group.Id = long.Parse(id);
                        groupService.DeleteGroup(group);
                        this.Response.Redirect(this.hid_sendurl.Value);
                    }
                }


                this.ddl_grouplevel.DataTextField = "Levelname";
                this.ddl_grouplevel.DataValueField = "Id";
                this.ddl_grouplevel.DataSource=levelservice.getAll();
                this.ddl_grouplevel.DataBind();


                if (action == ActionType.edit.ToString())
                {
                    Ecreate.Popedom.tGroup group = groupService.getById(long.Parse(id));
                    this.ddl_grouplevel.SelectedValue = group.Grouplevel.Id;
                    if (group.Adminflag == 1)
                    {
                        Response.Write("<script type=\"text/javascript\" language=\"javascript\">     alert('管理员组不允许修改!')　　; </script>");
                        this.Response.Redirect(this.hid_sendurl.Value);
                        return;
                    }
                    this.txt_name.Text = group.Groupname;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.hid_action.Value == ActionType.add.ToString())
            {
                string id = this.ddl_grouplevel.SelectedValue;
                Ecreate.Popedom.tGrouplevel grouplevel = levelservice.getById(id);
                Ecreate.Popedom.tGroup group = new Ecreate.Popedom.tGroup();
                group.Groupname = this.txt_name.Text;
                group.Adminflag = 0;
                group.Grouplevel = grouplevel;
                groupService.InsertGroup(group);
            }
            if (this.hid_action.Value == ActionType.edit.ToString())
            {


                string id = this.ddl_grouplevel.SelectedValue;
                Ecreate.Popedom.tGrouplevel grouplevel = levelservice.getById(id);

                Ecreate.Popedom.tGroup groupserach = groupService.getById(long.Parse(this.hid_id.Value));
                if (groupserach.Users.Count > 0)
                {
                    HtmlHelper.ShowAlertMessage(this, "组中包括用户，组级别不允许修改");
                    return;
                }
                Ecreate.Popedom.tGroup group = new Ecreate.Popedom.tGroup();
                group.Id = long.Parse(this.hid_id.Value);
                group.Groupname = this.txt_name.Text;
                group.Adminflag = 0;
                group.Grouplevel = grouplevel;
                groupService.UpdateGroup(group);
            }
            this.Response.Redirect(this.hid_sendurl.Value);
        }
    }
}
